package com.example.flutter_aygsh

import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant

import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import android.content.Intent
import java.io.File
import android.net.Uri
import android.content.ComponentName
import androidx.core.content.FileProvider

class MainActivity: FlutterActivity() {
    override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
        GeneratedPluginRegistrant.registerWith(flutterEngine);
        val channel = MethodChannel(flutterEngine.dartExecutor.binaryMessenger, CHANNEL);
        // channel.setMethodCallHandler{methodCall, result -> this.handleMethod(methodCall, result) };//设置方法监听
        channel.setMethodCallHandler { methodCall: MethodCall, result: MethodChannel.Result ->
            if (methodCall.method=="install") {
                    val path = methodCall.argument<String>("path")
                    val bundleId = methodCall.argument<String>("bundleId")
                    val file = File(path)
                    val fileUri = FileProvider.getUriForFile(this@MainActivity,
                    bundleId + ".fileprovider", file);
                    val it = Intent()
                    it.setAction(Intent.ACTION_VIEW)
                    it.setDataAndType(fileUri, "application/vnd.android.package-archive")
                    it.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
                    it.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)// 防止打不开应用
                    this@MainActivity.startActivity(it)
            }else  if (methodCall.method=="taobao") {
                   
                    val it = Intent()
                    it.setAction(Intent.ACTION_MAIN)
                            it.addCategory(Intent.CATEGORY_LAUNCHER)
 it.flags = Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED or Intent.FLAG_ACTIVITY_NEW_TASK
                    // it.addFlags(Intent.L)// 防止打不开应用
it.component = ComponentName("com.taobao.taobao","com.taobao.tao.TBMainActivity")
                    this@MainActivity.startActivity(it)
            }else {
                result.notImplemented()
            }
        }
    }
    companion object {
        private val CHANNEL = "com.example.flutter_aygsh/update"//渠道名
    }
}
